﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using System.Data.Common;

namespace WindowsFormsApplication1
{
    public partial class frmDTNhapdiemLan2 : Form
    {
        int hocky;
        string mamonhoc;
        string sql_Diem2;
        DataSet ds_Diem2;
        SqlConnection connection = new SqlConnection("Data Source = .; Initial Catalog = SM; Integrated Security = True");
        
        public frmDTNhapdiemLan2()
        {
            InitializeComponent();
            lbChonlop.Visible = false;
            lku_Chonlop.Visible = false;
            gridControl1.Visible = false;
            lbChonmonhoc.Visible = false;
        }

        DataSet Filldataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.Message);
            }
            return ds;
        }

        private void btnThucHien_Click(object sender, EventArgs e)
        {
            bool ktra = dxValidationProvider1.Validate();
            if (ktra)
            {
                hocky = Int32.Parse(txtHocky.Text);
                DataSet ds = Filldataset("Select MalopID, Tenlop from View_TKChatLuongSV where Namhoc = N'" + txtNamHoc.Text + "' and Hocky = '" + hocky + "' group by MalopID, Tenlop");
                int count = ds.Tables[0].Rows.Count;
                if (count == 0)
                    MessageBox.Show("Không có lớp nào được mở trong học kỳ này", "Thông báo");
                else
                {
                    lbChonlop.Visible = true;
                    lku_Chonlop.Visible = true;
                    gridControl1.Visible = true;
                    lbChonmonhoc.Visible = true;
                    lku_Chonlop.Properties.DataSource = ds.Tables[0];
                }
            }
            else
                MessageBox.Show("Bạn chưa nhập dữ liệu cần thiết", "Quản lý sinh viên", MessageBoxButtons.OK, MessageBoxIcon.None);
       
        }

          public void get_gridviewDiem(string malop)
        {
            DataSet ds = Filldataset("select Mamonhoc, TenMonhoc from View_TKChatLuongSV where MalopID = N'" + malop + "' and Hocky = '" + txtHocky.Text + "' and Namhoc = '" + txtNamHoc.Text + "'group by Mamonhoc, TenMonhoc");
            gcol_Mamonhoc.FieldName = "Mamonhoc";
            gcol_Tenmonhoc.FieldName = "TenMonhoc";
            gridControl1.DataSource = ds.Tables[0];
        }

        private void lku_Chonlop_EditValueChanged(object sender, EventArgs e)
        {
            string malop = lku_Chonlop.EditValue.ToString();
            get_gridviewDiem(malop);
        }

        #region
        //Tu luu diem L1 sau khi them
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            mamonhoc = gridView1.GetRowCellValue(e.RowHandle, gcol_Mamonhoc).ToString();
            sql_Diem2 = "exec PC_NhapDiemLan2 '" + txtNamHoc.Text + "', '" + hocky + "', '" + lku_Chonlop.Text + "', '" + mamonhoc + "'";
            ds_Diem2 = Filldataset(sql_Diem2);
            gCol_MaSV.FieldName = "MasvID";
            gCol_Holot.FieldName = "Holot";
            gCol_Ten.FieldName = "Ten";
            gCol_DiemL1.FieldName = "DiemL1";
            gCol_DiemL2.FieldName = "DiemL2";
            gCol_DiemL3.FieldName = "DiemL3";
            gridControl2.DataSource = ds_Diem2.Tables[0];           
        }

        int update(string sql, DataSet dataset)
        {
            int effect = 0;
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                SqlCommandBuilder builder = new SqlCommandBuilder(adap);
                adap.UpdateCommand = builder.GetUpdateCommand();
                effect = adap.Update(dataset);
                dataset.AcceptChanges();
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.Message);
            }
            return effect;
        }

        private void gridView2_RowUpdated(object sender, RowObjectEventArgs e)
        {
            ColumnView view = gridControl2.FocusedView as ColumnView;
            if (view.PostEditor() && view.UpdateCurrentRow())
            {
                update(sql_Diem2, ds_Diem2);
            }
        }

        #endregion   
    }
}
